package com.hhdys.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhdys.dao.FunctionMapper;
import com.hhdys.model.Function;
import com.hhdys.model.FunctionExample;
import com.hhdys.util.CacheUtil;
import com.hhdys.util.Constant;

@Service
public class FunctionServiceImpl implements FunctionService {
	private static Logger log=Logger.getLogger(FunctionServiceImpl.class);
	@Autowired
	private FunctionMapper dao;

	/**
	 * @return the dao
	 */
	public FunctionMapper getDao() {
		return dao;
	}

	/**
	 * @param dao
	 *            the dao to set
	 */
	public void setDao(FunctionMapper dao) {
		this.dao = dao;
	}

	@Override
	public boolean addFunction(Function func) {
		return dao.insert(func) > 0;
	}

	@Override
	public List<Function> getFunctionsByPageId(int pageId) {
		FunctionExample example = new FunctionExample();
		FunctionExample.Criteria criteria = example.createCriteria();
		criteria.andPageIdEqualTo(pageId);
		return dao.selectByExample(example);
	}

	@Override
	public boolean delFunction(int id) {
		return dao.deleteByPrimaryKey(id) > 0;
	}

	@Override
	public Function getFuncById(int id) {
		Function func = (Function )CacheUtil.get(Constant.FUNCCACHE, String.valueOf(id));
		if(func!=null){
			log.debug("使用功能点缓存=====");
			return func;
		}
		func=dao.selectByPrimaryKey(id);
		return func;
	}
}
